<template>
  <div>
    <el-form
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="设置密码" prop="userPassword" class="pass input">
        <el-input v-model="userPassword" placeholder="请设置登录密码" type="password" />
        <i class="iconfont icon-password icon" />
        <i class="iconfont icon-eye eye" />
        <div class="box">
          <div v-show="getPassState" class="error-msg">{{ hintGetPass }}</div>
        </div>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword" class="pass input">
        <el-input v-model="checkPassword" placeholder="请确认登录密码" type="password" />
        <i class="iconfont icon-password icon" />
        <i class="iconfont icon-close-eyes eye" />
        <div class="box">
          <div v-show="affirmPassState" class="error-msg">{{ hintAffirmPass }}</div>
        </div>
      </el-form-item>
    </el-form>
    <button class="last" @click="last">上一步</button>
    <button class="business" @click="register">提交注册</button>
    <el-checkbox v-model="checked" class="check">注册代表您同意</el-checkbox>
    <span v-for="(item, index) in agreements" :key="index" class="clause" @click="toAgreement(item.id)" v-text="'《'+item.protocolTitle+'》'">《阿奇数用户服务条款》</span>
  </div>
</template>
<script>
import { listProtocol } from '@/api/register'
import { passwordReg } from '@/js/regex'
import { personRegister } from '@/api/register/personal'
export default {
  name: 'SecondPersonalRegister',
  props: {
    codeForm: {
      require: true,
      type: Object
    }
  },
  data() {
    return {
      checked: false,
      agreements: [],
      userPassword: undefined,
      checkPassword: undefined,
      getPassState: false,
      affirmPassState: false,
      hintGetPass: '',
      hintAffirmPass: ''
    }
  },
  mounted() {
    listProtocol().then(res => {
      this.agreements = res.data
    })
  },
  methods: {
    // 查看服务协议
    toAgreement(id) {
      this.$router.push(`/agreement/${id}`)
    },
    last() {
      this.$emit('last')
    },
    validPassword() {
      if (!this.userPassword) {
        this.getPassState = true
        this.hintGetPass = '请先填写密码'
        return false
      }
      if (!passwordReg.test(this.userPassword)) {
        this.getPassState = true
        this.hintGetPass = '密码格式不正确：6-18位字符，只能并且必须包含字母和数字'
        return false
      }
      if (!this.checkPassword) {
        this.getPassState = false
        this.affirmPassState = true
        this.hintAffirmPass = '请先确认密码'
        return false
      }
      if (this.userPassword !== this.checkPassword) {
        this.getPassState = false
        this.affirmPassState = true
        this.hintAffirmPass = '两次密码不一致'
        return false
      }
      return true
    },
    register() {
      if (this.validPassword()) {
        if (!this.checked) {
          this.$message('请先阅读条款服务并同意')
          return
        }
        const { userName, uuid } = this.codeForm
        const headers = { uuid }
        const { userPassword } = this
        const data = { userName, userPassword }
        personRegister(headers, data).then(res => {
          if (res.data) {
            this.$message('注册成功')
          } else {
            this.$message('注册失败,请稍后再试')
          }
          // }).catch(error => {
        }).catch(() => {
          this.$message('注册失败,请稍后再试')
        })
      }
    }
  }
}
</script>
<style scoped lang="scss">
.registered {
  padding-top: 88px;
  padding-bottom: 87px;
  background: #f5f5f5;
  .type {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 14px 0 30px;
    .choose {
      display: flex;
      justify-content: space-between;
      .type-choose {
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 140px;
        height: 40px;
        background: rgba(255, 255, 255, 1);
        margin-right: 14px;
        border-bottom: 0;
      }
      .type-choose-1 {
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;
        width: 140px;
        height: 40px;
        background: rgba(245, 245, 245, 1);
        border: 1px solid rgba(229, 229, 229, 1);
        border-bottom: 0;
        margin-right: 14px;
      }
    }
    .register{
      color: #0198CD;
      cursor: pointer;
    }
  }
  .registered-content {
    background: #ffffff;
    .contents {
      display: flex;
      justify-content: space-between;
      height: 600px;
      .left {
        padding-top: 71px;
        margin-left: 90px;
        .icon{
          position: absolute;
          left: 14px;
          font-size: 22px;
          top: 2px;
        }
        .box{
          height: 28px;
          line-height: 28px;
        }
        .error-msg{
          text-align: left;
          padding-left: 0px;
          color: #f56c6c;
        }
        .eye{
          position: absolute;
          right: 90px;
          color: #C7C7CC;
          font-size: 22px;
        }
        .business{
          width: 400px;
          height: 42px;
          background: #FF3B30;
          color: #ffffff;
          text-align: center;
          line-height: 42px;
          cursor: pointer;
          margin-left: 100px;
          border-radius: 3px;
          font-size: 18px;
        }
        .last{
          width: 400px;
          height: 42px;
          background: #ffffff;
          margin-bottom: 15px;
          border: 1px solid #C7C7CC;
          color: #A7A7AA;
          text-align: center;
          line-height: 42px;
          cursor: pointer;
          margin-left: 100px;
          border-radius: 3px;
          font-size: 18px;
        }
        .check{
          margin-top: 18px;
          margin-left: 100px;
          font-size: 12px;
          .clause{
            color: #FF3B30;
          }
        }
      }
      .right {
        .registered-img{
          padding: 114px 40px 0 0;
        }
      }
    }
  }
  .input{
    margin-bottom: 0;
  }
  /deep/ .registered-content{
    .el-input{
      width: 400px;
      height: 42px;
      .el-input__inner{
        height: 42px;
        padding-left: 47px;
      }
    }
  }
}
</style>
